<template>
  <Description title="定时关机" content="定时关机" />
  <div>
    <a-form :model="formState" name="validate_other" v-bind="formItemLayout" @finishFailed="onFinishFailed"
      @finish="onFinish">
      <a-form-item label="程序等级" name="type" :rules="[{ required: true, message: '请选择!' }]">
        <a-radio-group v-model:value="formState.type">
          <a-radio value="system">系统程序定时</a-radio>
          <a-radio value="snyl">软件程序定时</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item name="time" label="关机时间" :rules="[{ required: true, message: '请选择关机时间!' }]">
        <a-date-picker v-model:value="formState.time" format="YYYY-MM-DD HH:mm:ss" valueFormat="x" placeholder="关机时间"
          :disabled-date="disabledDate" :disabled-time="disabledDateTime"
          :show-time="{ defaultValue: dayjs('00:00:00', 'HH:mm:ss') }" />
      </a-form-item>

      <a-form-item :wrapper-col="{ span: 12, offset: 6 }">
        <a-button type="primary" html-type="submit">创建</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import dayjs, { Dayjs } from "dayjs";
import { useTaskStore } from "@/stores/task";
import { reactive } from "vue";
import Description from '@/components/Description.vue'
const taskStore = useTaskStore();
interface FormState {
  time: string;
  type: string;
}
const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 14 },
};

const formState = reactive<FormState>({
  time: "",
  type: "",
});
const disabledDate = (current: Dayjs) => {
  // Can not select days before today and today
  return current && current.isBefore(dayjs(), "day");
};
const range = (start: number, end: number) => {
  const result: number[] = [];

  for (let i = start; i < end; i++) {
    result.push(i);
  }

  return result;
};
const disabledDateTime = () => {
  const current = dayjs();
  const currentHour = current.hour();
  const currentMinute = current.minute();
  const currentSecond = current.second();

  return {
    disabledHours: () => {
      return range(0, currentHour);
    },
    disabledMinutes: (selectedHour: any) => {
      if (selectedHour === currentHour) {
        return range(0, currentMinute);
      }
      return [];
    },
    disabledSeconds: (selectedHour: any, selectedMinute: any) => {
      if (selectedHour === currentHour && selectedMinute === currentMinute) {
        return range(0, currentSecond);
      }
      return [];
    },
  };
};

function onFinish(values: FormState) {
  console.log("Success:", values);
  const time = (Number(values.time) - Date.now())

  if (values.type == "system") {
    // window.ipcRenderer.invoke("exec", `shutdown /s /t ${Math.floor(time / 1000)}`);
    return
  }

  taskStore.data.push({
    id: Date.now(),
    type: 'setTimeout',
    url: 'timedShutdown',
    task: setTimeout(() => {
      // window.ipcRenderer.invoke("exec", `shutdown /s /t 0`);
    }, time),
    description: `电脑将在 ${dayjs(Number(values.time)).format("MM-DD HH:mm:ss")} 关机`
  });

}



const onFinishFailed = (errorInfo: any) => {
  console.log("Failed:", errorInfo);
};
</script>

<style></style>
